package com.jxm;

import org.testng.annotations.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author jiaxXM
 * @date 2024/6/21
 */
public class Test1 {

    @Test
    void testTakeWhile (){
        List<Integer> list = List.of(10,20,30,40,30,20,10);
        //takeWhile 返回从开头开始的尽可能多的元素
        list.stream().takeWhile(t->t<30).forEach(System.out::println);
        //过滤所有元素
        System.out.println("------------------");
        list.stream().filter(t->t<30).forEach(System.out::println);
        System.out.println("------------------");
        List<Integer> list2 = Arrays.asList(1,2,3,4,5,6,7);
        list2.stream().takeWhile(t->t<7).forEach(System.out::println);
    }

    @Test
    void testDropWhile (){
        List<Integer> list = Arrays.asList(10,20,30,40,30,20,10);
        list.stream().dropWhile(t->t<40).forEach(System.out::println);
        System.out.println("==================");
        List<Integer> list2 = Arrays.asList(1,2,3,4,5,6,7);
        list2.stream().dropWhile(t->t<7).forEach(System.out::println);
    }

    //新增了stream()方法，将一个 Optional 对象转换为一个(可能是空的)Stream 对象。
    @Test
    void testOptional(){
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        Optional<List<String>> optional = Optional.ofNullable(list);
        optional.stream().flatMap(x->x.stream()).limit(2).forEach(System.out::println);
        optional.stream().map(x->x.stream()).limit(2).forEach(System.out::println);

        list.stream().limit(2).forEach(System.out::println);

    }

    @Test
    void testHTTP() throws Exception{
        HttpClient client = HttpClient.newHttpClient();   //直接创建一个新的HttpClient
        //现在只需要构造一个Http请求实体，就可以让客户端帮助我们发送出去了（实际上就跟浏览器访问类似）
//        HttpRequest request = HttpRequest.newBuilder().uri(new URI("https://www.baidu.com")).build();
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:11002/rpc/sys/users/01HCH7E2K8fDeB3LC4iBL0E0AA")).build();
        //现在就可以把请求发送出去了，注意send方法后面还需要一个响应体处理器（内置了很多）这里我们选择ofString直接吧响应实体转换为String字符串
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        //响应实体
        System.out.println(response.body());
    }

    @Test
    void testswitch(){
        String res = null;
        var score = 10;
        switch (score) {
            case 10, 9 -> {
                System.out.println("=======================");
                res = "优秀";
            }
            case 8, 7 ->res =  "良好";
            case 6 -> res =  "及格";
            default -> res =  "不及格";
        };
        System.out.println(res);
    }
}
